var Rson = require('Rson');
var SpriteLoader = require('RaiSpriteLoader');

var self;
var canSePlay;

cc.Class({
    extends: cc.Component,

    properties: {
        
        group: 'G',
        checkboard: cc.Sprite,
        wormNode: cc.Node,
        stateBar: cc.Sprite,
        eventLayer: cc.Node,
        pop: cc.Node,
        enemyTeam: [cc.Sprite],
        myTeam: [cc.Sprite],

        myTerminalPanel: cc.Node,
        enemyTerminalPanel: cc.Node,

        p_mass: cc.Prefab,
        p_click_b: cc.Prefab,
        p_click_g: cc.Prefab,
        p_fire: cc.Prefab,
        p_danger: cc.Prefab,
        p_honor: cc.Prefab,
        P_winback: cc.Prefab,
        P_rv: cc.Prefab,

        bullet: cc.Prefab,
        gamestart: false,
        vnum: 0,
        lnum: 0,
        terminalSwitch: [String][Boolean],
        arrowCanPress: true,
        myServerSize: 0,
        enemyServerSize: 0,
        captureL: 0,
        captureV: 0,
    },

    // use this for initialization
    onLoad: function () {
        cc.log("回顾场景加载");
        
        this.plistUrl1 = 'Texture/Tex1'; //plist的url
        this.currentChess = null;
        this.tips = cc.find('Canvas/fitLayer/stateBar/tips').getComponent(cc.Label);
        this.stateBar = cc.find('Canvas/fitLayer/stateBar').getComponent(cc.Sprite);
        this.buttonBar = cc.find('Canvas/fitLayer/buttonBar').getComponent('buttonBar');
        //0.1s1次不断回滚的文字
        this.animeLabel = cc.find('Canvas/fitLayer/buttonBar/label').getComponent(cc.Label);
        let animeCount = 0;
        this.schedule(function(){
            let add = 'Reviewing';
            for(let i = 0;i < animeCount%10;i++)
            {
                if(i%2 == 0)
                    add += ' ';
                else
                    add += '.';
            }
            self.animeLabel.string = add;
            animeCount++;
        }, 0.1);
        
        self = this;

        // sound
        this.loadSound();
        //设置bgm开关
        // this.isSound = cc.sys.localStorage.getItem('bgmSwitch') == 'true';
        // if(this.isSound){
        //     cc.audioEngine.setMusicVolume(1);
        // }else{
        //     cc.audioEngine.setMusicVolume(0);
        // }
        //音效开关
        canSePlay = cc.sys.localStorage.getItem('seSwitch');

        // 准备完成后解析战报
        this.battleSteps = JSON.parse(cc.historyString);
        // 战报位置指针
        this.point = -1;//永远指向当前位置
        this.bottomPoint = 0;//指向战报读取的最低点，开始后当前位置永远在此之上
        // 已执行过的步骤数组
        this.doneTable = new Array();

        // 循环遍历battleSteps，去除中间的无效项
        let steps = this.battleSteps;
        for(let i = steps.length - 1;i >= 0;i--)
        {
            // 移除玩家请求命令
            if(steps[i].indexOf("\"name\":") > 0)
                steps.splice(i,1);
            // 去除23、24、51、61、71、81、106命令
            else if(steps[i].indexOf("\"code\":\"51\"") > 0 || steps[i].indexOf("\"code\":\"61\"") > 0 || steps[i].indexOf("\"code\":\"71\"") > 0
            || steps[i].indexOf("\"code\":\"23\"") > 0 || steps[i].indexOf("\"code\":\"24\"") > 0 || steps[i].indexOf("\"code\":\"81\"") > 0 
            || steps[i].indexOf("\"code\":\"106\"") > 0)
                steps.splice(i,1);
            // 获得最终全棋子身份
            else if(steps[i].indexOf("\"code\":\"91") > 0 && steps[i].indexOf("identify"))
            {
                this.finalCode = Rson.decode(steps[i]).data;
            }
        }
        // 连续执行命令直到一方回合开始
        while(true){
            if(steps[this.point+1].indexOf("\"code\":\"21") <= 0)
                break;
            this.onWSMsg(this.battleSteps[++this.point]);
            this.bottomPoint = this.point;
        }
        //初始化终端卡记录
        this.terminalSwitch['G'] = [];
        this.terminalSwitch['B'] = [];
        //等待2秒后开始正式战报
        setTimeout(() => {
            if(this.point < this.battleSteps.length - 1)
                this.onWSMsg(this.battleSteps[++this.point]);

        }, 2000);

        // 添加场景点击反馈侦听
        let listenerObj = {
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            onTouchBegan: function (touch, event) {
                let pos = self.node.convertToNodeSpaceAR(touch.getLocation());
                self.showEffect('click'+self.group, pos.x, pos.y);

                // 吞噬标签为true则吞没事件,否则开启吞噬标签并计时恢复//作用是让每次有效点击之间间隔0.5秒
                if(self.swallowTag)
                    event.stopPropagation();
                else{
                    self.swallowTag = true;
                    self.scheduleOnce(function(){ self.swallowTag = false; }, 0.5);
                }
                return true; /*这里必须要写 return true*/
            },

            onTouchMoved: function (touch, event) {
                //0.5秒显示一次点击特效防止卡顿
                if(self.swallowTag == true)
                    return;
                let pos = self.node.convertToNodeSpaceAR(touch.getLocation());
                self.showEffect('click'+self.group, pos.x, pos.y);
            }
        };
        // 绑定单点触摸事件
        this.clickListener = cc.eventManager.addListener(listenerObj, this.eventLayer);
    },

    //加载声音pfb
    loadSound: function(callback)
    {
        // 加载sound
        cc.loader.loadRes("Prefab/Sound", (err, pfb) => {
            self.sound = cc.instantiate(pfb).getComponent('Sound');
            self.node.addChild(self.sound.node);
            self.curTag = 0;
            self.curBgm = 0;
            self.bgm = [self.sound.bgm01, self.sound.bgm02, self.sound.bgm03, 
                self.sound.bgm04, self.sound.bgm05, self.sound.bgm06];

            // 计时器循环播放背景音乐
            cc.audioEngine.stopMusic();
            self.bgmScheduleFunc = function(){
                if(cc.sys.localStorage.getItem('bgmSwitch') != 'true')
                    return;
                if(!cc.audioEngine.isMusicPlaying())
                {
                    cc.log('bgm播放', self.bgm.length);
                    if(self.bgm.length > self.curBgm)
                        cc.audioEngine.playMusic(self.bgm[self.curBgm++], false);
                    else{
                        self.curBgm = 0;
                        cc.audioEngine.playMusic(self.bgm[self.curBgm++], false);
                    }
                }
            };
            self.schedule(self.bgmScheduleFunc, 5);

            if(callback != null)
                callback();
        });
    },

    // 运行或退回某一步骤
    // @isFoward : bool  若是前进一步则为true，后退为false
    runStep: function(isFoward){
        if(isFoward && this.point > this.bottomPoint && this.point < this.battleSteps.length - 1){
            this.onWSMsg(this.battleSteps[++this.point]);

        }else if(!isFoward && this.point > this.bottomPoint + 1 && this.point < this.battleSteps.length + 1){
            this.onBack(this.doneTable.pop());
            this.point--;
        }
    },

    // 给 返回ui 的回调函数
    onArrowBack: function(){
        if(self.arrowCanPress){
            // 点击前进/后退按钮后有0.5秒冷却
            self.arrowCanPress = false;
            self.runStep(false);
            self.schedule(function(){
                self.arrowCanPress = true;
            }, 0.5, 1);
        }
    },
    // 给 前进ui 的回调函数
    onArrowAhead: function(){
        if(self.arrowCanPress){
            // 点击前进/后退按钮后有0.5秒冷却
            self.arrowCanPress = false;
            self.runStep(true);
            self.schedule(function(){
                self.arrowCanPress = true;
            }, 0.5, 1);
        }
    },

    // 退出回放系统
    closePop: function(){
        var pop = this.pop.getComponent('Pop');
        pop.show('reviewClose');
    },

    // 模拟战斗时websocket回调
    onWSMsg: function(data){
        var data = Rson.decode(data);
        var msg = data.data;
        cc.log("Forward: code: ", data.code, ' data: ', msg, ' to: ', data.to);

        let myChess;
        let enemyChess;
        let eatenChess;
        switch(data.code){
            case '21':
                //tips
                self.setTips('游戏开始');
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'room':msg.room, 'myName':msg.myName, 'enemyName':msg.enemyName, 'group':msg.group});

                //搜索指定信息中开始信息个数
                var count = 0;
                for(let i in this.battleSteps)
                {
                    let step = this.battleSteps[i];
                    if(step.indexOf("\"code\":\"21") > 0)
                        count++;
                }
                if(count == 1)
                    self.checkedUser = 1;
                cc.log('Count: '+ count);

                // 游戏开始
                //检查当前信息是否发给自己，若两边都不是自己则直接开始
                if(data.to != cc.UID && !self.checkedUser){
                    self.checkedUser = 1;
                    return;
                }else if(data.to != cc.UID && self.checkedUser === 1){
                    self.checkedUser = 2;
                    self.UID = data.to;
                }else if(!self.checkedUser || self.checkedUser === 1){
                    self.UID = data.to;
                    self.checkedUser = 3;
                }else
                    return;
                cc.log('checkedUser: '+self.checkedUser);

                self.room = msg.room;
                self.setRoom(msg.room);
                self.myName = msg.myName;self.enemyName = msg.enemyName;
                self.setPlayerNames(msg.myName, msg.enemyName);
                self.group = msg.group;
                self.enemyGroup = msg.group=='G'?'B':(msg.group=='B'?'G':null);
                self.changeMap(msg.group);
                //初始化终端卡记录面板
                self.myTerminalRecord = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
                self.enemyTerminalRecord = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
                self.myTerminalRecord.init(self.group);
                self.enemyTerminalRecord.init(self.enemyGroup);

                // 是否开局显示棋子身份?
                if(self.checkedUser == 3)
                {
                    self.checkedUser = true;
                    let idens = self.finalCode.identify;
                    for(let i = 0;i < idens.length;i++)
                    {
                        for(let k = 0;k < 8;k++)
                        {
                            let iden = idens[i];
                            if(iden['group'] == self.group){
                                let mc = self.myTeam[k].getComponent('Chess');
                                if(mc.grpNum == iden['no']){
                                    mc.changeType(iden['type']?iden['type']:'bottom', false);
                                    break;
                                }
                            }else{
                                let ec = self.enemyTeam[k].getComponent('Chess');
                                if(ec.grpNum == iden['no']){
                                    ec.changeType(iden['type']?iden['type']:'bottom', false);
                                    break;
                                }
                            }
                        }
                    }
                }else
                    self.checkedUser = false;
                self.playSound('begin');

            break;

            case '31':
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target});

                // 超速回线 反馈
                if(data.to == self.UID)
                {
                    //己方
                    if(!msg.test){
                        //tips
                        self.setTips('取消Line Boost');
                        // 音效
                        self.playSound('line');
                        // 摘除已装备的超速回线
                        self.setLineBoost(self.group, msg.target, false);
                        // 回合结束
                        self.turnEnd();
                    }else
                        self.runStep(true);
                }else{
                    //敌方
                    if(!msg.test)
                    {
                        //tips
                        self.setTips('取消Line Boost');
                        // 音效
                        self.playSound('line');
                        // 摘除已装备的超速回线
                        self.setLineBoost(self.enemyGroup, msg.target, false);
                    }else
                        self.runStep(true);
                }
                
            break;

            case '33':
                //tips
                self.setTips('装备Line Boost');
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target});

                // 音效
                self.playSound('line');
                self.playSound('lb');
                // 超速回线 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        // 装备超速回线
                        self.setLineBoost(self.group, msg.target, true);
                        // 回合结束
                        self.turnEnd();
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                    {
                        // 装备超速回线
                        self.setLineBoost(self.enemyGroup, msg.target, true);
                    }
                }
            break;

            case '41':
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target});

                // 防火墙 反馈
                if(data.to == self.UID){
                    if(!msg.test){
                        //tips
                        self.setTips('取消Fire Wall');
                        // 音效
                        self.playSound('wall');
                        // 移除己方现有防火墙
                        self.setFireWall(self.group, msg.target['x'], msg.target['y'], false);
                        // 回合结束
                        self.turnEnd();
                    }else
                        self.runStep(true);
                }else{
                    if(!msg.test)
                    {
                        //tips
                        self.setTips('取消Fire Wall');
                        // 音效
                        self.playSound('wall');
                        // 移除己方现有防火墙
                        self.setFireWall(self.enemyGroup, 9-msg.target['x'], 9-msg.target['y'], false);
                    }else
                        self.runStep(true);
                }
                
            break;

            case '43':
                //tips
                self.setTips('放置Fire Wall');
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target});

                // 音效
                self.playSound('wall');
                self.playSound('fw');
                // 防火墙 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        // 添加wall
                        self.setFireWall(self.group, msg.target['x'], msg.target['y'], true);
                        // 回合结束
                        self.turnEnd();

                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                        // 添加wall
                        self.setFireWall(self.enemyGroup, 9-msg.target['x'], 9-msg.target['y'], true);
                }
                
            break;

            case '53':
                //tips
                self.setTips('发动Virus Check');
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result});

                // 音效
                self.playSound('check');
                self.playSound('vc');
                // 探查 结束
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        self.setVirusCheck(self.enemyGroup, msg.target, msg.result);
                        // 回合结束
                        self.turnEnd();
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                        self.setVirusCheck(self.group, msg.target, msg.result);
                }
                
            break;

            case '63':
                //tips
                self.setTips('发动404 Not Found');
                // 音效
                self.playSound('exchange');
                self.playSound('nf');
                // 交换 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        // 记录已执行命令
                        let c_no1 = self.myTeam[msg.no1-1].getComponent('Chess');
                        let c_no2 = self.myTeam[msg.no2-1].getComponent('Chess');
                        self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 
                        'no1':msg.no1, 'no2':msg.no2, 'check':msg.check, 'checked1':c_no1.checked?c_no1.type:false, 'checked2':c_no2.checked?c_no2.type:false});
                        // 交换
                        self.switchChess(self.group, msg.no1, msg.no2, msg.check, null);
                        // 回合结束
                        self.turnEnd();
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test){
                        // 记录已执行命令
                        let c_no1 = self.enemyTeam[msg.no1-1].getComponent('Chess');
                        let c_no2 = self.enemyTeam[msg.no2-1].getComponent('Chess');
                        self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 
                        'no1':msg.no1, 'no2':msg.no2, 'check':msg.check, 'checked1':c_no1.checked?c_no1.type:false, 'checked2':c_no2.checked?c_no2.type:false});
                        // 交换
                        self.switchChess(self.enemyGroup, msg.no1, msg.no2, msg.check, null);
                    }
                }
            break;

            case '203':
                //tips
                self.setTips('发动Random Verification');
                // 音效
                self.playSound('Verification');
                self.playSound('rv');
                //随机验证 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        // 记录已执行命令
                        self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'useGroup':self.group == 'G'?'G':'B'});
                        //显示特效
                        self.showEffect('myRv');
                        //向对方展示可能的验证位置
                        self.setRandoms(msg.target, 1);
                        if(self.VerificationPos)
                            self.setRandoms(new Array(self.VerificationPos), 2);//标记最终选择的验证位置
                        //记录随机验证出现点
                        if(self.group == 'G')
                            self.randomPosesG = msg.target;
                        else
                            self.randomPosesB = msg.target;
                        //确认一方使用终端卡
                        self.terminalSwitch[self.group]['rv'] = true;
                        let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
                        terminalPanel.setShownTerminal('randomverification');
                        // 回合结束
                        self.turnEnd();
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                    {
                        // 记录已执行命令
                        self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'useGroup':self.group == 'G'?'B':'G'});
                        //翻转对方的棋盘坐标
                        for(let i in msg.target)
                        {
                            let pos = msg.target[i];
                            pos.x = 9-pos.x;
                            pos.y = 9-pos.y;
                        }
                        //显示特效
                        self.showEffect('enemyRv');
                        //向对方展示可能的验证位置
                        self.setRandoms(msg.target, 1);
                        //记录随机验证出现点
                        if(self.group == 'B')
                            self.randomPosesG = msg.target;
                        else
                            self.randomPosesB = msg.target;
                        //确认一方使用终端卡
                        self.terminalSwitch[self.enemyGroup]['rv'] = true;
                        let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
                        terminalPanel.setShownTerminal('randomverification');
                    }
                }
            break;

            case '211':
                //零日攻击 确认
                //tips
                self.setTips('Zero-Day Attack发动');

                // 记录已执行命令
                myChess = self.myTeam[msg.target-1].getComponent('Chess');
                enemyChess = self.enemyTeam[msg.target-1].getComponent('Chess');
                //普通移动
                if(msg.result['type'] == 1)
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY} });

                //吃子
                else if(msg.result['type'] == 2){
                    eatenChess = (data.to==self.UID?self.enemyTeam:self.myTeam)[msg.result['eno']-1].getComponent('Chess');
                    
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY, 
                    'echecked':eatenChess.checked, 'elb':msg.result['moveLB'], 'sbOpen':msg.result['sbOpen'], 'rvRmSb':msg.result['rvRemoveSB'], 
                    'rvRmLb':msg.result['rvRemoveLB'], 'captureL':self.captureL, 'captureV':self.captureV} });

                //进server
                }else{
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY, 
                    'echecked':(data.to==self.UID?myChess:enemyChess).checked, 'lb':msg.result['moveLB'], 'captureL':self.captureL, 'captureV':self.captureV} });
                }

                // 音效
                self.playSound('zda');
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        //记录选中棋子显示特效
                        let chess = self.myTeam[msg.target - 1];
                        self.playZDAAnime(chess);
                        self.setZeroDayAttack(self.group, data, msg);
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                    {
                        //记录选中棋子显示特效
                        let chess = self.enemyTeam[msg.target - 1];
                        self.playZDAAnime(chess);
                        self.setZeroDayAttack(self.enemyGroup, data, msg);
                    }
                }
            break;

            case '231':
                //沙盒测试 确认

                //tips
                self.setTips('Sand Box 发动');
                // 记录已执行命令
                let cmd = {'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target};
                // 音效
                self.playSound('sb');
                //装备的其他装备解除
                if(msg.target.removeLB)
                {
                    cmd.removeLB = true;
                    self.doneTable.push(cmd);
                    self.setLineBoost(msg.target.group, msg.target.no, false);
                }
    
                if(data.to == cc.UID)
                {
                    if(msg.test)
                    {
                        // 装备沙盒测试
                        self.setSandBox(msg.target.group, msg.target.no, true);
                        //标记tc使用情况
                        self.terminalSwitch[self.group]['sb'] = false;
                        //确认一方使用终端卡
                        let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
                        terminalPanel.setShownTerminal('sandbox');
                    }
                }else{
                    if(msg.test)
                    {
                        // 装备沙盒测试
                        self.setSandBox(msg.target.group, msg.target.no, false);
                        //标记tc使用情况
                        self.terminalSwitch[self.enemyGroup]['sb'] = false;
                        //确认对方使用终端卡
                        let enemyTerminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
                        enemyTerminalPanel.setShownTerminal('sandbox');
                    }
                }
            break;

            case '73':
                //tips
                self.setTips('移动棋子');
                // 记录已执行命令
                myChess = self.myTeam[msg.target-1].getComponent('Chess');
                enemyChess = self.enemyTeam[msg.target-1].getComponent('Chess');
                //普通移动
                if(msg.result['type'] == 1)
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY} });

                //吃子
                else if(msg.result['type'] == 2){
                    eatenChess = (data.to==self.UID?self.enemyTeam:self.myTeam)[msg.result['eno']-1].getComponent('Chess');
                    
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY, 
                    'echecked':eatenChess.checked, 'elb':msg.result['moveLB'], 'sbOpen':msg.result['sbOpen'], 'rvRmSb':msg.result['rvRemoveSB'], 
                    'rvRmLb':msg.result['rvRemoveLB'], 'captureL':self.captureL, 'captureV':self.captureV} });

                //进server
                }else{
                    self.doneTable.push({'code':data.code, 'to':data.to, 'test':msg.test, 'target':msg.target, 'result':msg.result, 
                    'origin':{'x':(data.to==self.UID?myChess:enemyChess).posX, 'y':(data.to==self.UID?myChess:enemyChess).posY, 
                    'checked':(data.to==self.UID?myChess:enemyChess).checked, 'lb':msg.result['moveLB'], 'sb':msg.result['moveLB'], 'captureL':self.captureL, 'captureV':self.captureV} });
                }

                self.onMoveChess(data, msg);
            break;

            case '91':
                // 记录已执行命令
                self.doneTable.push({'code':data.code, 'to':data.to, 'winner':msg.winner});

                // 游戏结束
                // 停止bgm回调并静音
                // self.unschedule(self.bgmScheduleFunc);
                // cc.audioEngine.pauseMusic();

                if(!self.checkedUser){
                    // 翻开全部棋子
                    for(let i = 0;i < msg.identify.length;i++)
                    {
                        for(let k = 0;k < 8;k++)
                        {
                            let iden = msg.identify[i];
                            if(iden['group'] == self.enemyGroup){
                                let ec = self.enemyTeam[k].getComponent('Chess');
                                if(ec.grpNum == iden['no']){
                                    ec.changeType(iden['type']);
                                    break;
                                }
                            }else{
                                let mc = self.myTeam[k].getComponent('Chess');
                                if(mc.grpNum == iden['no']){
                                    mc.changeType(iden['type']);
                                    break;
                                }
                            }
                        }
                    }
                    self.checkedUser = true;
                }
                // 结束演出
                if(msg.winner == self.UID)
                {
                    self.setTips("游戏结束，"+self.myName+"获胜");
                }else{
                    self.setTips('游戏结束，'+self.enemyName+'获胜');
                }
            break;
        }
    },

    // 回退函数
    onBack: function(data){
        var msg = data;
        cc.log("Back to: code: ", data.code, ' data: ', msg, ' to: ', data.to);

        switch(data.code){

            case '31':
                // 超速回线 反馈
                if(data.to == self.UID)
                {
                    //己方
                    if(!msg.test){
                        //tips
                        self.setTips('撤销 取消Line Boost');
                        // 摘除已装备的超速回线
                        self.setLineBoost(self.group, msg.target, true);
                    }else
                        self.runStep(false);
                }else{
                    //敌方
                    if(!msg.test){
                        //tips
                        self.setTips('撤销 取消Line Boost');
                        // 摘除已装备的超速回线
                        self.setLineBoost(self.enemyGroup, msg.target, true);
                    }else
                        self.runStep(false);
                }
                
            break;

            case '33':
                //tips
                self.setTips('撤销 使用Line Boost');
                // 超速回线 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                        // 装备超速回线
                        self.setLineBoost(self.group, msg.target, false);
                }else{
                    if(msg.test)
                        // 装备超速回线
                        self.setLineBoost(self.enemyGroup, msg.target, false);
                }
            break;

            case '41':
                // 防火墙 反馈
                if(data.to == self.UID){
                    if(!msg.test){
                        //tips
                        self.setTips('撤销 取消Fire Wall');
                        // 移除己方现有防火墙
                        self.setFireWall(self.group, msg.target['x'], msg.target['y'], true);
                    }else
                        self.runStep(false);
                }else{
                    if(!msg.test){
                        //tips
                        self.setTips('撤销 取消Fire Wall');
                        // 移除己方现有防火墙
                        self.setFireWall(self.enemyGroup, 9-msg.target['x'], 9-msg.target['y'], true);
                    }else
                        self.runStep(false);
                }
                
            break;

            case '43':
                //tips
                self.setTips('撤销 使用Fire Wall');
                // 防火墙 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                        // 添加wall
                        self.setFireWall(self.group, msg.target['x'], msg.target['y'], false);
                }else{
                    if(msg.test)
                        // 添加wall
                        self.setFireWall(self.enemyGroup, 9-msg.target['x'], 9-msg.target['y'], false);
                }
                
            break;

            case '53':
                //tips
                self.setTips('撤销 使用Virus Check');
                // 探查 结束
                if(data.to == self.UID)
                {
                    if(msg.test)
                        self.unsetVirusCheck(self.enemyGroup, msg.target);
                }else{
                    if(msg.test)
                        self.unsetVirusCheck(self.group, msg.target);
                }
                
            break;

            case '63':
                //tips
                self.setTips('撤销 使用 404 Not Found');
                // 交换 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                        self.switchChess(self.group, msg.no1, msg.no2, msg.check, new Array(msg.checked1, msg.checked2));
                }else{
                    if(msg.test)
                        self.switchChess(self.enemyGroup, msg.no1, msg.no2, msg.check, new Array(msg.checked1, msg.checked2));
                }
            break;

            case '203':
                //tips
                self.setTips('撤销 使用 Random Verification');
                //随机验证 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        self.setRandoms(msg.target, 0);//隐藏可能的验证位置
                        if(self.group == 'G')
                            self.randomPosesG = null;
                        else
                            self.randomPosesB = null;
                            self.terminalSwitch[self.group]['rv'] = false;
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                    {
                        self.setRandoms(msg.target, 0);//隐藏可能的验证位置
                        if(self.group == 'B')
                            self.randomPosesG = null;
                        else
                            self.randomPosesB = null;
                        self.terminalSwitch[self.enemyGroup]['rv'] = false;
                    }
                }
            break;

            case '211':
                //tips
                self.setTips('撤销 使用 Zero-Day Attack');
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        //撤回终端卡使用记录
                        this.terminalSwitch[self.group]['zda'] = false;
                        //将棋子移回原位
                        let origin = msg.origin;
                        let result = msg.result;
                        self.moveChess(self.group, msg.target, origin['x'], origin['y']);
                        //如果吃了子
                        if(result['type'] == 2)
                        {
                            let script = self.enemyTeam[result['eno']-1].getComponent('Chess');
                            if(origin['elb'])
                            {
                                script.setLineBoost(true);
                                self.terminalSwitch[self.enemyGroup]['lb'] = true;
                            }
                            //复原敌方棋子被吃掉前的翻面情况
                            if(origin['echecked'] && self.checkedUser)
                                script.setCheckTag(true);
                            else if(self.checkedUser == false && !origin['echecked'])
                                script.changeType('bottom');
                            //复原因sb翻面
                            if(origin['sbOpen'])
                            {
                                let myChess = self.myTeam[msg.target-1].getComponent('Chess');
                                myChess.setCheckTag(false);
                            }
                            
                            self.myCaptureL = origin['captureL'];
                            self.myCaptureV = origin['captureV'];
                            self.setCaptureState(self.group, origin['captureL'], origin['captureV']);
                            
                            self.moveChess(self.enemyGroup, result['eno'], result['x'], result['y']);
                        }
                        // 若是移入database
                        else if(result['type']==3){
                            // 按server现存棋子数移动棋子位置
                            self.myServerSize--;

                            let script = self.myTeam[msg.target-1].getComponent('Chess');

                            if(origin['lb']){
                                self.terminalSwitch[self.group]['lb'] = true;
                                script.setLineBoost(true);
                            }

                            if(origin['checked']){
                                script.setCheckTag(true);
                            }
                        }
                        //如果触发了 [随机验证] 就把所有可能的验证标记移除
                        if(result['verificated'])
                        {
                            if(result['effectValication'] == 'G')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesG, 1);
                            }
                            else if(result['effectValication'] == 'B')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesB, 1);
                            }
                            else
                            {
                                //消除全部随机验证点
                                self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 1);
                            }
                            //标记棋子身份复原
                            let script = self.myTeam[msg.target-1].getComponent('Chess');
                            script.setCheckTag(false);
                            //撤销 移除装备终端卡
                            if(result['rvRemoveLB'])
                            {
                                self.setLineBoost(self.group, msg.target, true);
                                self.terminalSwitch[self.group]['lb'] = true;
                            }
                            else if(result['rvRemoveSB'])
                            {
                                self.setSandBox(self.group, msg.target, true);
                            }
                        }
                    }else
                        cc.log(msg.error);
                }else{
                    if(msg.test)
                    {
                        //撤回终端卡使用记录
                        this.terminalSwitch[self.enemyGroup]['zda'] = false;
                        let result = msg.result;
                        let origin = msg.origin;
                        self.moveChess(self.enemyGroup, msg.target, origin['x'], origin['y']);

                        // 若移动模式为进攻（吃了别的棋子）则移除掉对应棋子
                        if(result['type']==2){
                            self.enemyCaptureL = origin['captureL'];
                            self.enemyCaptureV = origin['captureV'];
                            self.setCaptureState(self.enemyGroup, result['captureL'], result['captureV']);
                            self.moveChess(self.group, result['eno'], 9-result['x'],9-result['y']);

                            let script = self.myTeam[result['eno']-1].getComponent('Chess');

                            if(origin['elb']){
                                self.terminalSwitch[self.group]['lb'] = true;
                                script.setLineBoost(true);
                            }
                            //复原敌方棋子被吃掉前的翻面情况
                            if(origin['echecked']){
                                script.setCheckTag(true);
                            }
                            //复原因sb翻面
                            if(origin['sbOpen'])
                            {
                                let myChess = self.myTeam[msg.target-1].getComponent('Chess');
                                if(self.checkedUser)
                                    myChess.setCheckTag(false);
                                else
                                    myChess.changeType('bottom');
                            }

                        }else if(result['type']==3){
                            // 按server现存棋子数移动棋子位置
                            self.enemyServerSize--;

                            let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                            
                            if(origin['lb'])
                            {
                                script.setLineBoost(true);
                                self.terminalSwitch[self.enemyGroup]['lb'] = true;
                            }

                            if(origin['checked'] && self.checkedUser)
                                script.setCheckTag(true);
                        }
                        //如果触发了 [随机验证] 就把所有可能的验证标记移除
                        if(result['verificated'])
                        {
                            if(result['effectValication'] == 'G')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesG, 1);
                            }
                            else if(result['effectValication'] == 'B')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesB, 1);
                            }
                            else
                            {
                                //消除全部随机验证点
                                self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 1);
                            }
                            //标记棋子身份复原
                            let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                            if(script.checked)
                                script.setCheckTag(false);
                            else
                                script.changeType('bottom');
                            //撤销 移除装备终端卡
                            if(result['rvRemoveLB'])
                            {
                                self.setLineBoost(self.enemyGroup, msg.target, true);
                            }
                            else if(result['rvRemoveSB'])
                            {
                                self.setSandBox(self.enemyGroup, msg.target, true);
                            }
                        }
                    }
                }
                break;

            case '231':
                //tips
                self.setTips('撤销 使用 Sand Box');
                self.setSandBox(msg.target.group, msg.target.no, false);
                //复原因sb移除的装备
                if(msg.removeLB)
                    self.setLineBoost(msg.target.group, msg.target.no, true);
                break;

            case '73':
                //tips
                self.setTips('撤销 移动棋子');
                // 棋子移动 确认
                if(data.to == self.UID)
                {
                    if(msg.test)
                    {
                        let result = msg.result;
                        let origin = msg.origin;
                        // 将棋子移回原位
                        self.moveChess(self.group, msg.target, origin['x'], origin['y']);

                        // 若移动模式为进攻（吃了别的棋子）则移除掉对应棋子
                        if(result['type']==2){

                            let script = self.enemyTeam[result['eno']-1].getComponent('Chess');

                            if(origin['elb'])
                            {
                                script.setLineBoost(true);
                                self.terminalSwitch[self.enemyGroup]['lb'] = true;
                            }
                            //复原敌方棋子被吃掉前的翻面情况
                            if(origin['echecked'] && self.checkedUser)
                                script.setCheckTag(true);
                            else if(self.checkedUser == false && !origin['echecked'])
                                script.changeType('bottom');
                            //复原因sb翻面
                            if(origin['sbOpen'])
                            {
                                let myChess = self.myTeam[msg.target-1].getComponent('Chess');
                                myChess.setCheckTag(false);
                            }
                            
                            self.myCaptureL = origin['captureL'];
                            self.myCaptureV = origin['captureV'];
                            self.setCaptureState(self.group, origin['captureL'], origin['captureV']);
                            
                            self.moveChess(self.enemyGroup, result['eno'], result['x'], result['y']);
                        }
                        // 若是移入database
                        if(result['type']==3){
                            // 按server现存棋子数移动棋子位置
                            self.myServerSize--;

                            let script = self.myTeam[msg.target-1].getComponent('Chess');

                            if(origin['lb']){
                                self.terminalSwitch[self.group]['lb'] = true;
                                script.setLineBoost(true);
                            }
                            else if(origin['sb'])
                            {
                                script.setSandBox(true);
                            }

                            if(origin['checked']){
                                script.setCheckTag(true);
                            }
                        }
                        //如果触发了 [随机验证] 就把所有可能的验证标记移除
                        if(result['verificated'])
                        {
                            if(result['effectValication'] == 'G')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesG, 1);
                            }
                            else if(result['effectValication'] == 'B')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesB, 1);
                            }
                            else
                            {
                                //消除全部随机验证点
                                self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 1);
                            }
                            //标记棋子身份复原
                            let script = self.myTeam[msg.target-1].getComponent('Chess');
                            script.setCheckTag(false);
                            //撤销 移除装备终端卡
                            if(result['rvRemoveLB'])
                            {
                                self.setLineBoost(self.group, msg.target, true);
                                self.terminalSwitch[self.group]['lb'] = true;
                            }
                            else if(result['rvRemoveSB'])
                            {
                                self.setSandBox(self.group, msg.target, true);
                            }
                        }
                    }
                }else{
                    if(msg.test)
                    {
                        let result = msg.result;
                        let origin = msg.origin;
                        self.moveChess(self.enemyGroup, msg.target, origin['x'], origin['y']);

                        // 若移动模式为进攻（吃了别的棋子）则移除掉对应棋子
                        if(result['type']==2){
                            self.enemyCaptureL = origin['captureL'];
                            self.enemyCaptureV = origin['captureV'];
                            self.setCaptureState(self.enemyGroup, result['captureL'], result['captureV']);
                            self.moveChess(self.group, result['eno'], 9-result['x'],9-result['y']);

                            let script = self.myTeam[result['eno']-1].getComponent('Chess');

                            if(origin['elb']){
                                self.terminalSwitch[self.group]['lb'] = true;
                                script.setLineBoost(true);
                            }
                            //复原敌方棋子被吃掉前的翻面情况
                            if(origin['echecked']){
                                script.setCheckTag(true);
                            }
                            //复原因sb翻面
                            if(origin['sbOpen'])
                            {
                                let enemyChess = self.enemyTeam[msg.target-1].getComponent('Chess');
                                if(self.checkedUser)
                                    enemyChess.setCheckTag(false);
                                else
                                    enemyChess.changeType('bottom');
                            }

                        }else if(result['type']==3){
                            // 按server现存棋子数移动棋子位置
                            self.enemyServerSize--;

                            let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                            
                            if(origin['lb'])
                            {
                                script.setLineBoost(true);
                                self.terminalSwitch[self.enemyGroup]['lb'] = true;
                            }

                            if(origin['checked'] && self.checkedUser)
                                script.setCheckTag(true);
                        }
                        //如果触发了 [随机验证] 就把所有可能的验证标记移除
                        if(result['verificated'])
                        {
                            if(result['effectValication'] == 'G')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesG, 1);
                            }
                            else if(result['effectValication'] == 'B')
                            {
                                //恢复一边的随机验证点
                                self.setRandoms(self.randomPosesB, 1);
                            }
                            else
                            {
                                //消除全部随机验证点
                                self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 1);
                            }
                            //标记棋子身份复原
                            let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                            if(script.checked)
                                script.setCheckTag(false);
                            else
                                script.changeType('bottom');
                            //撤销 移除装备终端卡
                            if(result['rvRemoveLB'])
                            {
                                self.setLineBoost(self.enemyGroup, msg.target, true);
                            }
                            else if(result['rvRemoveSB'])
                            {
                                self.setSandBox(self.enemyGroup, msg.target, true);
                            }
                        }
                    }
                }
            break;

            case '91':
                self.runStep(false);
            break;
        }
    },

    // 结束游戏演出
    showEndEffect: function(isWinner){
        if(isWinner)
        {
            // 音效
            self.playSound('win');
        }else{
            // 音效
            this.playSound('lose');
        }
    },


    // 根据地图数组初始化棋盘
    initChessBoard: function(mapData){
        cc.log('init ChessBoard');
    },

    // 改变棋盘样式。不同阵营有不同的棋盘样式
    // @type: 阵营类别
    changeMap: function(type){
        
        this.group = type;
        if(type == 'G'){
            SpriteLoader.load('checkboard1', sprite => {
                this.checkboard.spriteFrame = sprite;
            });
            SpriteLoader.load('stateBar1', sprite => {
                this.stateBar.spriteFrame = sprite;
            });
        }else{
            SpriteLoader.load('checkboard2', sprite => {
                this.checkboard.spriteFrame = sprite;
            });
            SpriteLoader.load('stateBar2', sprite => {
                this.stateBar.spriteFrame = sprite;
            });
        }
        // 给所有己方棋子改变类组别
        for(var key in this.myTeam)
        {
            let node = this.myTeam[key].node;
            let chess = node.getComponent('Chess');
            chess.changeGroup(type);
        }
        // 给所有对方棋子改变类组别
        for(var key in this.enemyTeam)
        {
            let node = this.enemyTeam[key].node;
            let chess = node.getComponent('Chess');
            chess.changeGroup(type=='G'?'B':'G');
        }
    },
    
    // 显示确认框
    // @type: 确认框种类
    showPop: function(type){
        var pop = this.pop.getComponent('Pop');
        pop.show(type);
    },

    // 设置提示信息
    // @str: 提示信息文本
    setTips: function(str){
        this.tips.string = str;
    },

    // 设置房间号
    // @num: 房间号
    setRoom: function(num){
        var label = cc.find('room', this.stateBar.node).getComponent(cc.Label);
        label.string = num;
    },

    // 设置玩家名
    // @myName: 己方玩家名
    // @enemyName: 对方玩家名
    setPlayerNames: function(myName, enemyName){
        var label1 = cc.find('myName', this.stateBar.node).getComponent(cc.Label);
        var label2 = cc.find('enemyName', this.stateBar.node).getComponent(cc.Label);
        label1.string = myName;
        label2.string = enemyName;
    },

    // 设置终端卡剩余状态显示
    // @group: 组别
    // @cl: 捕获的link数
    // @cv: 捕获的virus数
    setCaptureState(group, cl, cv){
        var label;
        if(this.group == group)
            label = cc.find('myTerminalState', this.stateBar.node).getComponent(cc.Label);
        else
            label = cc.find('enemyTerminalState', this.stateBar.node).getComponent(cc.Label);
        label.string = 'Link: '+cl+' Virus: '+cv;
    },

    // 实现交换效果
    // @c1: 第一个指定棋子在数组中的下标+1
    // @c2: 第二个指定棋子在数组中的下标+1
    // @isSwitch: 是否交换
    // @checkBack: 是否回退check状态 数组
    switchChess: function(group, c1, c2, isSwitch, checkBack){
        // 获得指定chess对象
        var chess1, chess2, team;
        team = this.group==group?this.myTeam:this.enemyTeam;
        
        chess1 = team[c1-1].node; chess2 = team[c2-1].node;

        var c1Script = chess1.getComponent('Chess'), c2Script = chess2.getComponent('Chess');
        var pos1 = chess1.getPosition(), pos2 = chess2.getPosition();
        var seq1, seq2, func1, func2, switchEnd, lbSet1=false, lbSet2=false;

        // 记录两枚棋子装备lineboost的状况
        if(isSwitch)
        {
            if(c1Script.hasLineBoost)
                lbSet2=true;
            else if(c2Script.hasLineBoost)
                lbSet1=true;
            //交换棋子坐标
            let tmpx = c1Script.posX;
            let tmpy = c1Script.posY;
            c1Script.posX = c2Script.posX;
            c1Script.posY = c2Script.posY;
            c2Script.posX = tmpx;
            c2Script.posY = tmpy;
        }else{
            if(c1Script.hasLineBoost)
                lbSet1=true;
            else if(c2Script.hasLineBoost)
                lbSet2=true;
        }

        // 回调函数用以去掉switch、check标记并重置敌方棋子至背面并设置line boost交换情况
        func1 = cc.callFunc(()=>{
            c1Script.setSwitchTag(false);

            if(!checkBack || !checkBack[0]){
                if(!self.checkedUser)
                    c1Script.changeTypeImmediately('bottom');
                else
                    c1Script.setCheckTag(false);
            }else{
                if(!self.checkedUser)
                    c1Script.changeTypeImmediately(checkBack[0]);
                else
                    c1Script.setCheckTag(true);
            }
            
            c1Script.setLineBoost(false);
        }, this);
        func2 = cc.callFunc(()=>{
            c2Script.setSwitchTag(false);
            
            if(!checkBack || !checkBack[1]){
                if(!self.checkedUser)
                    c2Script.changeTypeImmediately('bottom');
                else
                    c2Script.setCheckTag(false);
            }else{
                if(!self.checkedUser)
                    c2Script.changeTypeImmediately(checkBack[1]);
                else
                    c2Script.setCheckTag(true);
            }

            c2Script.setLineBoost(false);
        }, this);

        switchEnd = cc.callFunc(()=>{
            c1Script.setLineBoost(lbSet1);
            c2Script.setLineBoost(lbSet2);
        });

        if(isSwitch){
            seq1 = cc.sequence(cc.spawn(c1Script.bezierMove(), func1), cc.moveTo(0.22, pos2), switchEnd);
            seq2 = cc.sequence(cc.spawn(c2Script.bezierMove(), func2), cc.moveTo(0.22, pos1));
        }else{
            seq1 = cc.sequence(cc.spawn(c1Script.bezierMove(), func1), cc.moveTo(0.22, pos1), switchEnd);
            seq2 = cc.sequence(cc.spawn(c2Script.bezierMove(), func2), cc.moveTo(0.22, pos2));
        }
        // 开始演出
        chess1.runAction(seq1);
        chess2.runAction(seq2);

        if(this.group==group)
        {
            //确认一方使用终端卡
            this.terminalSwitch[group]['nf'] = true;
            let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('notfound');
        }else{
            //确认一方使用终端卡
            this.terminalSwitch[group]['nf'] = true;
            let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('notfound');
        }
    },

    // 实现棋子移动
    // @group: 阵营/颜色
    // @c: 棋子在数组中的下标
    // @x, y: 移动坐标
    moveChess: function(group, c, x, y){
        cc.log('棋子移动', group, c, x, y);
        var chess = group==this.group?this.myTeam[c-1] : this.enemyTeam[c-1];
        chess.getComponent('Chess').moveTo(x, y);
    },

    // 加速回线效果
    // @group: 组别
    // @c: 指定的棋子
    // @isSwitch: 开启还是关闭
    setLineBoost: function(group, c, isSwitch){
        var chess;
        if(this.group == group)
        {
            chess = this.myTeam[c-1].node.getComponent('Chess');
            //确认一方使用终端卡
            let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('lineboost');
            this.terminalSwitch[group]['lb'] = isSwitch;
        }
        else{
            chess = this.enemyTeam[c-1].node.getComponent('Chess');
            //确认一方使用终端卡
            let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('lineboost');
            this.terminalSwitch[group]['lb'] = isSwitch;
        }
        chess.setLineBoost(isSwitch);
    },

    // 防火墙效果
    // @bx, by: 指定的空格位置坐标
    // @isSwitch: 开启还是关闭
    // @group: fireWall的种类
    setFireWall: function(group, bx, by, isSwitch){
        var board = this.checkboard.node;
        let block = cc.find('line'+by+'/block'+bx, board);

        if(group == this.group)
        {
            //确认一方使用终端卡
            let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('firewall');
            this.terminalSwitch[group]['fw'] = isSwitch;
        }
        else{
            //确认一方使用终端卡
            let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('firewall');
            this.terminalSwitch[group]['fw'] = isSwitch;
        }
        block.getComponent('block').setFireWall(isSwitch, group);
    },

    // 探查器效果
    // @group: 棋子的种类
    // @c: 指定的显示身份的棋子
    // @type: 棋子的真实身份
    setVirusCheck: function(group, c, type){
        let team = group==this.group?this.myTeam:this.enemyTeam;
        var chess = team[c-1].node.getComponent('Chess');
        
        if(self.checkedUser)
            chess.setCheckTag(true);
        else{
            chess.changeType(type);
        }
        if(group==this.group)
        {
            //确认一方使用终端卡
            let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('viruscheck');
        }
        else{
            //确认一方使用终端卡
            let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('viruscheck');
        }
        this.terminalSwitch[group]['vc'] = true;
    },

    // 取消探查器效果
    unsetVirusCheck: function(group, c){
        let team = group==this.group?this.myTeam:this.enemyTeam;
        var chess = team[c-1].node.getComponent('Chess');
        
        if(self.checkedUser)
            chess.setCheckTag(false);
        else{
            chess.changeType('bottom');
        }
        this.terminalSwitch[group]['vc'] = false;
    },

    // 零日攻击效果
    // @group: 组别
    // @c: 指定的棋子
    // @x, y: 坐标
    setZeroDayAttack: function(group, data, msg){
        if(group == this.group)
        {
            //确认一方使用终端卡
            let terminalPanel = self.myTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('zerodayattack');
        }else{
            //确认对方使用终端卡
            let terminalPanel = self.enemyTerminalPanel.getComponent('EnemyTerminalPanel');
            terminalPanel.setShownTerminal('zerodayattack');
        }
        this.terminalSwitch[group]['zda'] = true;
        //模拟棋子移动
        self.onMoveChess(data, msg);
    },

    // 设置验证格标记
    // @blocks: 记录可通过区域的数组
    setRandoms: function(poses, mode){
        cc.log('关闭所有通过效果');
        for(let key in this.myTeam){
            let script = this.enemyTeam[key].getComponent('Chess');
            script.clearFocusTag();
        }

        cc.log('开启指定位置效果');
        for(let key in poses)
        {
            let pos = poses[key];
            cc.log('指定位置', pos['x'], pos['y']);
            let block = cc.find('line'+pos['y']+'/block'+pos['x'], this.checkboard.node).getComponent('block');
            block.setRandomVerification(mode);
        }
    },

    // 发送弹幕
    // @str: 要发送的弹幕内容
    // @group: 弹幕发送方，分为'G''B''V'三种
    shotBullet: function(str, group){
        
        var bullet = cc.instantiate(this.bullet);
        var ran = Math.ceil(Math.random()*9) - 5;
        var scale = (Math.ceil(Math.random()*21) - 11) / 100 + 1;
        var winWidth = cc.winSize.width;
        var time = 3.5 + 0.1 * str.length + ran * 0.05;
        bullet.getComponent(cc.Label).string = str;
        bullet.width = str.length * 25 * scale;
        bullet.height = 40 * scale;
        bullet.setPosition(winWidth / 2 + bullet.width / 2, 40*ran);

        switch(group){
            case 'G':
                bullet.color = cc.color(255, 255, 0);
            break;
            case 'B':
                bullet.color = cc.color(0, 0, 255);
            break;
            case 'V':
                bullet.color = cc.color(168, 168, 168);
            break;
        }
        var seq = cc.sequence(cc.moveBy(time, -winWidth - bullet.width, 0), cc.callFunc(()=>{bullet.removeFromParent();}));
        cc.log(str, time);
        this.node.addChild(bullet);
        bullet.runAction(seq);
    },

    showEffect: function(type, x, y){
        let p;
        switch(type){
            case 'honor':
                p = cc.instantiate(this.p_honor);
                this.checkboard.node.addChild(p);
                p.setPosition(this.convertBoardToNodeXY(x,y));

                this.playSound('link');
            break;

            case 'danger':
                p = cc.instantiate(this.p_danger);
                this.checkboard.node.addChild(p);
                p.setPosition(this.convertBoardToNodeXY(x,y));

                this.playSound('virus');
            break;

            case 'clickB':
                p = cc.instantiate(this.p_click_b);
                this.node.addChild(p);
                p.setPosition(x, y);

                this.playSound('click');
            break;

            case 'clickG':
                p = cc.instantiate(this.p_click_g);
                this.node.addChild(p);
                p.setPosition(x, y);

                this.playSound('click');
            break;

            case 'fire':
                p = cc.instantiate(this.p_fire);
                this.node.addChild(p);

                p.setPosition(x, y);
            break;

            case 'myRv':
                //给棋盘上我方棋子处显示rv特效 3秒后销毁
                let myTeam = this.myTeam;
                for(let i in myTeam)
                {
                    p = cc.instantiate(this.P_rv);
                    let chess = myTeam[i].node.getComponent('Chess');
                    if(chess.posY >= 1 && chess.posY <= 8)
                        chess.setTempEffect(p, 3);
                }
            break;

            case 'enemyRv':
                //给棋盘上敌方棋子处显示rv特效 3秒后销毁
                let enemyTeam = this.enemyTeam;
                for(let i in enemyTeam)
                {
                    p = cc.instantiate(this.P_rv);
                    let chess = enemyTeam[i].node.getComponent('Chess');
                    if(chess.posY >= 1 && chess.posY <= 8)
                        chess.setTempEffect(p, 3);
                }
            break;
            
        }
    },

    // 播放音效
    playSound: function(effect){
        //声音没加载完成时返回
        if(this.sound == null)
            return;
        //设置了不播放音效时返回
        if(!canSePlay)
            return;

        switch(effect){
            case 'link':
                cc.audioEngine.play(this.sound.e_link);
            break;

            case 'virus':
                cc.audioEngine.play(this.sound.e_virus);
            break;

            case 'line':
                cc.audioEngine.play(this.sound.e_line);
            break;

            case 'wall':
                cc.audioEngine.play(this.sound.e_wall);
            break;

            case 'check':
                cc.audioEngine.play(this.sound.e_check);
            break;

            case 'exchange':
                cc.audioEngine.play(this.sound.e_exchange);
            break;

            case 'Verification':
                cc.audioEngine.play(this.sound.e_Verification);
            break;

            case 'begin':
                cc.audioEngine.play(this.sound.e_begin);
            break;

            case 'move':
                cc.audioEngine.play(this.sound.e_move);
            break;

            case 'click':
                cc.audioEngine.play(this.sound.e_click);
            break;

            case 'lose':
                cc.audioEngine.play(this.sound.e_lose);
            break;

            case 'win':
                cc.audioEngine.play(this.sound.voice_win);
            break;

            case 'lb':
                cc.audioEngine.play(this.sound.voice_lb);
            break;

            case 'fw':
                cc.audioEngine.play(this.sound.voice_fw);
            break;

            case 'vc':
                cc.audioEngine.play(this.sound.voice_vc);
            break;

            case 'nf':
                cc.audioEngine.play(this.sound.voice_nf);
            break;

            case 'rv':
                cc.audioEngine.play(this.sound.voice_rv);
            break;

            case 'zda':
                cc.audioEngine.play(this.sound.voice_zda);
            break;

            case 'sb':
                cc.audioEngine.play(this.sound.voice_sb);
            break;
        }
    },

    // 将点击坐标转化为棋盘坐标
    getBoardXY: function(pos){
        let boardX=null, boardY=null;
        if(pos.y>=215 && pos.y<=775){
            boardX = Math.round((pos.x+7.5)/70);
            boardY = 9-Math.round((pos.y-180)/70);
        }

        if(boardX && boardY)
            return cc.p(boardX, boardY);
        else
            return null;
    },

    convertBoardToNodeXY: function(x, y){
        let nodeX=null, nodeY=null;
        if(y>=1 && y<=8){
            nodeX = -313 + 70*x;
            nodeY = 315 - 70*y;
        }

        if(nodeX && nodeY)
            return cc.p(nodeX, nodeY);
        else
            return null;
    },

    // 回合结束时处理所有侦听
    turnEnd:function(){
        cc.log('回合结束');
    },

    //移动棋子
    onMoveChess: function(data, msg)
    {
        // 音效
        self.playSound('move');
        // 棋子移动 确认
        if(data.to == self.UID)
        {
            if(msg.test)
            {
                let result = msg.result;
                if(result['type'] != 3)
                    self.moveChess(self.group, msg.target, result['x'], result['y']);

                // 若移动模式为进攻（吃了别的棋子）则移除掉对应棋子
                if(result['type']==2){
                    let eatenChess = eatenChess = (data.to==self.UID?self.enemyTeam:self.myTeam)[msg.result['eno']-1].getComponent('Chess');
                    let script = eatenChess;
                    //清除sb以外的标记
                    script.clearAllTag('sb');

                    if(self.checkedUser == false)
                        script.changeType(result['etype']);
                    
                    self.captureL = result['captureL'];
                    self.captureV = result['captureV'];
                    self.setCaptureState(self.group, result['captureL'], result['captureV']);
                    
                    self.moveChess(self.enemyGroup, result['eno'], result['ex'], result['ey']);
                    // 显示吃子时的粒子特效
                    if(result['etype']=='link')
                        self.showEffect('honor', result['x'], result['y']);
                    else
                        self.showEffect('danger', result['x'], result['y']);

                    //吃子的棋子装备了SandBoxTest
                    if(result['sbOpen'])
                    {
                        //棋子翻面
                        let myChess = self.myTeam[msg.target-1].getComponent('Chess');
                        myChess.setCheckTag(true);
                    }
                    //吃的棋子装备了lb
                    if(result['moveLB'])
                    {
                        self.terminalSwitch[self.enemyGroup]['lb'] = false;
                    }
                }
                // 若是移入database
                if(result['type']==3){
                    // 按server现存棋子数移动棋子位置
                    self.myServerSize++;
                    self.moveChess(self.group, msg.target, self.myServerSize, result['y']);

                    if(result['moveLB'])
                        self.terminalSwitch[self.enemyGroup]['lb'] = false;

                    let script = myChess;
                    //清除sb以外的标记
                    script.clearAllTag('sb');
                }
                //如果触发了 [随机验证] 就把所有可能的验证标记移除
                if(result['verificated'])
                {
                    self.setTips('触发Random Verification');
                    if(result['effectValication'] == 'G')
                    {
                        //消除一边的随机验证点
                        self.setRandoms(self.randomPosesG, 0);
                    }
                    else if(result['effectValication'] == 'B')
                    {
                        //消除一边的随机验证点
                        self.setRandoms(self.randomPosesB, 0);
                    }
                    else
                    {
                        //消除全部随机验证点
                        self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 0);
                    }
                    //标记棋子身份已被探查
                    let script = self.myTeam[msg.target-1].getComponent('Chess');
                    if(script.type != 'bottom')
                        script.setCheckTag(true);
                    else
                        script.changeType(result['moveType']);
                    //移除装备终端卡
                    if(result['rvRemoveLB'])
                    {
                        self.setLineBoost(self.group, msg.target, false);
                        self.terminalSwitch[self.group]['lb'] = false;
                    }
                    else if(result['rvRemoveSB'])
                    {
                        self.setSandBox(self.group, msg.target, false);
                    }
                }
                // 回合结束
                self.turnEnd();
            }else
                cc.log(msg.error);
        }else{
            if(msg.test)
            {
                let result = msg.result;
                if(result['type'] != 3)
                    self.moveChess(self.enemyGroup, msg.target, 9-result['x'], 9-result['y']);

                // 若移动模式为进攻（吃了别的棋子）则移除掉对应棋子
                if(result['type']==2){
                    self.captureL = result['captureL'];
                    self.captureV = result['captureV'];
                    self.setCaptureState(self.enemyGroup, result['captureL'], result['captureV']);
                    self.moveChess(self.group, result['eno'], 9-result['ex'],9-result['ey']);

                    if(self.checkedUser == false)
                    {
                        let eatenChess = eatenChess = (data.to==self.UID?self.enemyTeam:self.myTeam)[msg.result['eno']-1].getComponent('Chess');
                        eatenChess.changeType(result['etype']);
                    }
                    //吃的棋子装备了lb
                    if(result['moveLB'])
                    {
                        self.terminalSwitch[self.group]['lb'] = false;
                    }
                    //吃子的棋子装备了SandBoxTest
                    if(result['sbOpen'])
                    {
                        //棋子翻面
                        let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                        script.changeType(result['sbOpen']);
                    }

                    // 显示吃子时的粒子特效
                    if(result['etype']=='link')
                        self.showEffect('honor', 9-result['x'], 9-result['y']);
                    else
                        self.showEffect('danger', 9-result['x'], 9-result['y']);

                }else if(result['type']==3){
                    // 按server现存棋子数移动棋子位置
                    self.enemyServerSize++;
                    self.moveChess(self.enemyGroup, msg.target, self.enemyServerSize, 9-result['y']);

                    if(result['moveLB'])
                        self.terminalSwitch[self.enemyGroup]['lb'] = false;

                    let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                    //清除sb以外的标记
                    script.clearAllTag('sb');
                }
                //如果触发了 [随机验证] 就把所有可能的验证标记移除
                if(result['verificated'])
                {
                    self.setTips('触发Random Verification');
                    if(result['effectValication'] == 'G')
                    {
                        //消除一边的随机验证点
                        self.setRandoms(self.randomPosesG, 0);
                    }
                    else if(result['effectValication'] == 'B')
                    {
                        //消除一边的随机验证点
                        self.setRandoms(self.randomPosesB, 0);
                    }
                    else
                    {
                        //消除全部随机验证点
                        self.setRandoms(self.randomPosesG.concat(self.randomPosesB), 0);
                    }
                    //棋子翻到正面
                    let script = self.enemyTeam[msg.target-1].getComponent('Chess');
                    if(script.type != 'bottom')
                        script.setCheckTag(true);
                    else
                        script.changeType(result['moveType']);
                    //移除装备终端卡
                    if(result['rvRemoveLB'])
                    {
                        self.setLineBoost(self.enemyGroup, msg.target, false);
                    }
                    else if(result['rvRemoveSB'])
                    {
                        self.setSandBox(self.enemyGroup, msg.target, false);
                    }
                }
            }
        }
    },

    //播放zero-day Attack动画
    playZDAAnime: function(target)
    {
        if(this.zdaAnime == null)
            this.zdaAnime = cc.find('Canvas/fitLayer/ZeroDayAnime').getComponent(cc.Animation);
        this.zdaAnime.node.active = true;
        //移动到合适的坐标
        let worldPos = target.node.parent.convertToWorldSpaceAR(target.node.position);
        let localPos = this.zdaAnime.node.parent.convertToNodeSpaceAR(worldPos);
        this.zdaAnime.node.x = localPos.x;
        //播放动画
        this.zdaAnime.play();
    },

    // 沙盒测试效果
    // @group: 组别
    // @c: 指定的棋子
    // @judge: 装备/解除终端卡
    setSandBox: function(group, c, judge = true){
        if(judge)
        {
            let chess;
            if(this.group == group)
                chess = this.myTeam[c-1].node.getComponent('Chess');
            else
                chess = this.enemyTeam[c-1].node.getComponent('Chess');
            chess.setSandBox(true);
        }
        else 
            self.removeSandBox(group, c);
    },

    //移除沙盒测试效果
    removeSandBox: function(group, c){
        let chess;
        if(this.group == group)
            chess = this.myTeam[c-1].node.getComponent('Chess');
        else
            chess = this.enemyTeam[c-1].node.getComponent('Chess');
        chess.setSandBox(false);
    },
});